package Window;
/*
 * A kimeneti elemeket és értéküket megjelenítő panel. A kijelzők aktuális értékét 
 * jeleníti meg adott táblázatban. Az oszcilloszkópok táblázatában miden sorában 
 * található egy Details feliratú gomb, amelyre kattintva új ablak nyílik meg, 
 * amelyben grafikusan jelenik az adott oszcilloszkóp összes stabil és összes ideiglenes értéke.
 * */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class OutputPanel {
	
	//view peldany
	View view;
	
	//Panel meretek
	protected int panelWidth;
	protected int panelHeight;
	protected JPanel outputPanel;
	//Boxok a rendezeshez
	protected Box verticalBoxOfDisplay;
	protected Box verticalBoxOfScope;
	protected Box horizontalBox;
	
	//Tablazatok
	protected JTable displayTable;
	protected JTable scopeTable;
	
	//Konstruktor
	public OutputPanel(View view, Dimension windowSize){
		this.view = view;
		
		panelWidth = (int)(windowSize.width*0.5);
		panelHeight = (int)(windowSize.height*0.5);
		initPanelAndBox();
		initDisplay();
		initScope();
		setHorizontal();
		createBorderForPanel();
	}
	
	//Visszaadja a panelt
	public JPanel getPanel(){
		return outputPanel;
	}
	
	//Panel letrehozas
	private void initPanelAndBox(){
		BorderLayout border = new BorderLayout();
		outputPanel = new JPanel(border);
		
		Dimension panelSize = new Dimension(panelWidth, panelHeight);
		outputPanel.setMaximumSize(panelSize);
		outputPanel.setPreferredSize(panelSize);
		
		horizontalBox = Box.createHorizontalBox();
	}
	
	//Display tablazat letrehozas
	private void initDisplay(){
		verticalBoxOfDisplay = Box.createVerticalBox();
		
		JLabel displayLabel = new JLabel("Display informations");
		verticalBoxOfDisplay.add(displayLabel);
		
		initDisplayTable();
		JScrollPane displayScrollPane = new JScrollPane(displayTable);
		Dimension displaySize = new Dimension(panelWidth, (int)(panelHeight*0.8));
		displayScrollPane.setMaximumSize(displaySize);
		verticalBoxOfDisplay.add(displayScrollPane);
	}
	
	//Display JTable letrehozas
	private void initDisplayTable(){
		String[] columnNames = new String[2];
		columnNames[0] = "Display ID"; columnNames[1] = "LogicalValue";
		
		int[] displayValues = view.getDisplay();
		
		String[][] displayDatas = null;
		if( displayValues != null ){
			displayDatas = new String[displayValues.length][2];
			for(int act=0; act<displayValues.length; act++){
				displayDatas[act][0] = Integer.toString(act);
				displayDatas[act][1] = Integer.toString(displayValues[act]);
			}
			displayTable = new JTable(displayDatas, columnNames);
			displayTable.setEnabled(false);
		}
		else
			displayTable = null;
	}
	
	//Scope tabla letrehozas
	private void initScope(){
		verticalBoxOfScope = Box.createVerticalBox();
		
		JLabel scopeLabel = new JLabel("Scope informations");
		verticalBoxOfScope.add(scopeLabel);
		
		initScopeTable();
		JScrollPane scopeScrollPane = new JScrollPane(scopeTable);
		Dimension scrollSize = new Dimension(panelWidth, (int)(panelHeight*0.8));
		scopeScrollPane.setMaximumSize(scrollSize);
		verticalBoxOfScope.add(scopeScrollPane);
	}
	
	//Scope JTable letrehozas
	private void initScopeTable(){
		String[] columnNames = new String[2];
		columnNames[0] = "Scope ID"; columnNames[1] = "Check button";
		
		int[][] scopeValues = view.getStableScopeValues();
		
		if( scopeValues != null){
			Object[][] dataVector = new Object[scopeValues.length][2];
			for(int act=0; act<scopeValues.length; act++){
				dataVector[act][0] = Integer.toString(act);
				dataVector[act][1] = "Details";
			}
			
			DefaultTableModel dm = new DefaultTableModel();
			dm.setDataVector(dataVector, columnNames);
			
			
			scopeTable = new JTable(dm){
				public boolean isCellEditable(int row, int column){
					if( column == 1)
						return true;
					return false;
				}
				public boolean isCellSelected(int row, int column){
					return false;
				}
			};
			scopeTable.getColumn("Check button").setCellRenderer(new ButtonCellRenderer());
			scopeTable.getColumn("Check button").setCellEditor(new ButtonCellEditor());
			
			for(int act=0; act<scopeValues.length; act++){
				ButtonCellEditor buttonCellEditor= (ButtonCellEditor)scopeTable.getCellEditor(act, 1);
				JButton button = new JButton("Details");
				ALForScopeDisplayWindow aLForScopeDisplayWindow = new ALForScopeDisplayWindow(act);
				button.addActionListener(aLForScopeDisplayWindow);
				buttonCellEditor.setCellButton(button);
			}
		}
	}
	
	//horizontalBox letrehozas
	private void setHorizontal(){
		Dimension spaceOnLeftSide = new Dimension( (int)(panelWidth*0.06), panelHeight);
		horizontalBox.add(Box.createRigidArea(spaceOnLeftSide));
		
		horizontalBox.add(verticalBoxOfDisplay);
		
		Dimension spaceBetweenTables = new Dimension( (int)(panelWidth*0.06), panelHeight);
		horizontalBox.add(Box.createRigidArea(spaceBetweenTables));
		
		horizontalBox.add(verticalBoxOfScope);
		
		Dimension spaceOnRigthSide = new Dimension( (int)(panelWidth*0.06), panelHeight);
		horizontalBox.add(Box.createRigidArea(spaceOnRigthSide));
	}
	
	//Border letrehozas
	private void createBorderForPanel(){
		outputPanel.add(horizontalBox);
		outputPanel.setBorder(BorderFactory.createTitledBorder("Displays and Scopes"));
	}
	
	//Scope ablakanak letrehozasa
	private class ALForScopeDisplayWindow implements ActionListener{
		int nbOfScope;
		
		public ALForScopeDisplayWindow(int nbOfScope){
			super();
			this.nbOfScope = nbOfScope;
		}
		
		public void actionPerformed(ActionEvent e){
			ScopeDisplayWindow scopedis = new ScopeDisplayWindow(view, nbOfScope);
		}
	}
}
